package com.example.spring_aop.proxy;

import net.sf.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/7/24
 * Time: 17:53
 */
public class CGLibMethodInterceptor implements MethodInterceptor {

    // 目标对象/被代理对象
    private Object target;

    public CGLibMethodInterceptor(Object target) {
        this.target = target;
    }

//    @Override
//    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
//        // before
//        System.out.println("开始代理");
//        // 通过反射执行目标对象的目标方法
//        Object result = method.invoke(target, args);
//        // after
//        System.out.println("结束代理");
//        return result;
//    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, net.sf.cglib.proxy.MethodProxy methodProxy) throws Throwable {
        // before
        System.out.println("开始代理");
        // 通过反射执行目标对象的目标方法
        Object result = method.invoke(target, objects);
        // after
        System.out.println("结束代理");
        return result;
    }
}
